/**
 * API错误名称
 */
let ApiError = require('./ApiError');
let ErrorUtils = {};
ErrorUtils.UNKNOW_ERROR = "unknowError";
ErrorUtils.REQUEST_ERROR = "requestError";
ErrorUtils.AUTH_FAIL = "authFail";
ErrorUtils.USER_NOT_EXIST = "userNotExist";
ErrorUtils.USER_HAS_EXIST = "userhasExist";
ErrorUtils.ISBN_NOT_EXIST = "isbnNotExist";
ErrorUtils.BOOK_NOT_EXIST = "bookNotExist";
ErrorUtils.ISBN_INVALID = "isbnInvalid";
ErrorUtils.ID_INVALID = "idInvalid";
ErrorUtils.USERNAME_NOT_EXIST = "usernameNotExist";
ErrorUtils.PASSWORD_ERROR = "passwordError";
ErrorUtils.WECHAT_CODE_ERROR='wechatCodeError';
ErrorUtils.UPLOAD_FILE_NOT_EXIST='uploadFileNotExist';
ErrorUtils.DATA_NOT_EXIST='dataNotExist';
ErrorUtils.DATA_EXIST='dataExist';
ErrorUtils.APPID_NOT_MATCH='appidNotMatch';
ErrorUtils.DECODE_PASSWORD_ERROR='decodePasswordError';
ErrorUtils.DECODE_INFO_NOT_EXIST='decodeInfoNotExist';
ErrorUtils.ORDER_HAS_EXIST='orderHasExist';
ErrorUtils.ORDER_NOT_EXIST='orderNotExist';
ErrorUtils.ORDER_ID_NOT_EXIST='orderIdNotExist';
ErrorUtils.TWICE_PASSWORD_NOT_SAME = "twicePasswordNotSame";
ErrorUtils.PHONE_NOT_EXISTS = "phoneNotExist";
ErrorUtils.UPDATE_FAIL = "updateFail";
ErrorUtils.INSERT_FAIL = "insertFail";
ErrorUtils.VERIFY_CODE_ERROR = "verifyCodeError";
ErrorUtils.USERNAME_EXISTS = "usernameExists";
ErrorUtils.EMAIL_VERIFY_CODE_ERROR = "emailVerifyCodeError";
ErrorUtils.EMAIL_VERIFY_CODE_NOT_EXISTS = "emailVerifyCodeNotExists";
ErrorUtils.USER_BASE_NOT_EXISTS = "userBaseInfoNotExists";
ErrorUtils.OLD_PASSWORD_ERROR = "oldPasswordError";
ErrorUtils.APPLY_NOT_EXISTS = "applyNotExists";
ErrorUtils.APPLY_HAS_VERIFY = "applyHasVerify";
ErrorUtils.APPLY_SUCCESS = "applySuccess";
ErrorUtils.APPLY_FAIL = "applyFail";
ErrorUtils.VERIFY_FAIL = "verifyFail";
ErrorUtils.VERIFY_SUCCESS = "verifySussess";
ErrorUtils.NOT_PAY = "notPay";
ErrorUtils.SELECT_INFO_NOT_EXISTS = "selectInfoNotExists";


/**
 * API错误名称对应的错误信息
 */
const error_map = new Map();

error_map.set(ErrorUtils.UNKNOW_ERROR, {code: 2000, msg: '未知错误'});
error_map.set(ErrorUtils.REQUEST_ERROR, {code: 2001, msg: '请求错误'});
error_map.set(ErrorUtils.USER_NOT_EXIST, {code: 2002, msg: '用户不存'});
error_map.set(ErrorUtils.AUTH_FAIL, {code: 401, msg: '鉴权失败'});

//book
error_map.set(ErrorUtils.ISBN_NOT_EXIST, {code: 2101, msg: 'isbn不存在'});
error_map.set(ErrorUtils.BOOK_NOT_EXIST, {code: 2102, msg: 'isbn无效'});
error_map.set(ErrorUtils.ISBN_INVALID, {code: 2103, msg: '书籍不存在'});
error_map.set(ErrorUtils.ID_INVALID, {code: 2104, msg: 'id不存在'});

//login
error_map.set(ErrorUtils.USERNAME_NOT_EXIST, {code: 2201, msg: '用户不存在'});
error_map.set(ErrorUtils.PASSWORD_ERROR, {code: 2202, msg: '密码不正确'});
error_map.set(ErrorUtils.USER_HAS_EXIST, {code: 2203, msg: '用户已经存在'});

//wechat
error_map.set(ErrorUtils.WECHAT_CODE_ERROR, {code: 2301, msg: '微信code 错误'});
//upload
error_map.set(ErrorUtils.UPLOAD_FILE_NOT_EXIST, {code: 2401, msg: '上传文件不存在'});

//storage
error_map.set(ErrorUtils.DATA_NOT_EXIST, {code: 2501, msg: '数据不存在'});
error_map.set(ErrorUtils.DATA_EXIST, {code: 2502, msg: '数据存在'});
error_map.set(ErrorUtils.APPID_NOT_MATCH, {code: 2503, msg: 'appid不匹配'});
error_map.set(ErrorUtils.DECODE_PASSWORD_ERROR, {code: 2505, msg: '解密错误'});
error_map.set(ErrorUtils.DECODE_INFO_NOT_EXIST, {code: 2506, msg: '解密信息不存在'});


error_map.set(ErrorUtils.ORDER_HAS_EXIST, {code: 2601, msg: '订单已经存在'});
error_map.set(ErrorUtils.ORDER_NOT_EXIST, {code: 2601, msg: '订单不存在'});
error_map.set(ErrorUtils.ORDER_ID_NOT_EXIST, {code: 2602, msg: '订单id不存在'});
error_map.set(ErrorUtils.TWICE_PASSWORD_NOT_SAME, {code: 2603, msg: '两次密码不相同'});
error_map.set(ErrorUtils.OLD_PASSWORD_ERROR, {code: 2603, msg: '老密码错误'});
error_map.set(ErrorUtils.PHONE_NOT_EXISTS, {code: 2604, msg: '手机号不存在'});
error_map.set(ErrorUtils.INSERT_FAIL, {code: 2605, msg: '插入失败'});
error_map.set(ErrorUtils.UPDATE_FAIL, {code: 2606, msg: '更新失败'});
error_map.set(ErrorUtils.VERIFY_CODE_ERROR, {code: 2607, msg: '邮箱验证码错误'});
error_map.set(ErrorUtils.USERNAME_EXISTS, {code: 2608, msg: '用户名存在'});
error_map.set(ErrorUtils.EMAIL_VERIFY_CODE_ERROR, {code: 2608, msg: '邮箱验证码错误'});
error_map.set(ErrorUtils.EMAIL_VERIFY_CODE_NOT_EXISTS, {code: 2609, msg: '邮箱验证码不存在'});
error_map.set(ErrorUtils.USER_BASE_NOT_EXISTS, {code: 2611, msg: '用户基本信息不存在'});
error_map.set(ErrorUtils.APPLY_NOT_EXISTS, {code: 2610, msg: '申请信息不存在'});
error_map.set(ErrorUtils.APPLY_HAS_VERIFY, {code: 2612, msg: '申请中审核中'});
error_map.set(ErrorUtils.APPLY_SUCCESS, {code: 2613, msg: '申请成功'});
error_map.set(ErrorUtils.VERIFY_FAIL, {code: 2613, msg: '审核成功'});
error_map.set(ErrorUtils.VERIFY_SUCCESS, {code: 2613, msg: '审核失败'});
error_map.set(ErrorUtils.NOT_PAY, {code: 2613, msg: '未支付'});
error_map.set(ErrorUtils.SELECT_INFO_NOT_EXISTS, {code: 2614, msg: '查询信息不存在'});


//根据错误名称获取错误信息
ErrorUtils.getErrorInfo = (error_name) => {
    let error_info;
    if (error_name) {
        error_info = error_map.get(error_name);
    }
    //如果没有对应的错误信息，默认'未知错误'
    if (!error_info) {
        error_name = ErrorUtils.UNKNOW_ERROR;
        error_info = error_map.get(error_name);
    }
    return error_info;
};
ErrorUtils.throwApiError = (error_name) => {
    let error_info = ErrorUtils.getErrorInfo(error_name);
    //console.log(">>>>>>>%",error_info);x
    throw new ApiError(error_name, error_info);
};

module.exports = ErrorUtils;
